#include "ScenePrecompiled.hxx"
#include "InternalSceneImpl.hxx"
#include "InternalSceneStaticImpl.hxx"

#include "InternalSceneChildImpl.hxx"

Eifersucht::Scene::_Internal_impl::_Internal_impl( _In_ Eifersucht::ScenePtr _pRef ) : 
	_m_pScene(nullptr),
	_m_pRef(_pRef)
{
}

Eifersucht::Scene::_Internal_impl::~_Internal_impl()
{
	Safe_Destroy(_m_pScene);
}

bool Eifersucht::Scene::_Internal_impl::_Import_from_file( _In_z_ LPCSTR _szPath )
{
	Eifersucht::Bool bImportStatus, bStatus;
	Eifersucht::Int32 iFileMajor, iFileMinor, iFileRevision,
		iSdkMajor, iSdkMinor, iSdkRevision;

	Fbx::ImporterPtr pSdkImporter = Fbx::Importer::Create( 
		_Internal_static_impl::_s_pManager, 
		"InternalEifersuchtImporter" 
		);
	Fbx::Manager::GetFileFormatVersion(
		iSdkMajor, iSdkMinor, iSdkRevision
		);
	bImportStatus = pSdkImporter->Initialize( 
		_szPath, (-1), _Internal_static_impl::_s_pSettings 
		);
	pSdkImporter->GetFileVersion(
		iFileMajor, iFileMinor, iFileRevision
		);

	if ( !bImportStatus )
	{
// Debug output only
#ifdef REIGN_DEBUG
		Fbx::String fbxszError = pSdkImporter->GetStatus().GetErrorString();
		PrintToDebugWindowOnly8( "Eifersucht. InternalSceneImpl. Failed to init the fbx importer." );
		PrintToDebugWindowOnly8( fbxszError.Buffer() );
#endif
		pSdkImporter->Destroy();
		return false;
	}
// Debug output only
#ifdef REIGN_DEBUG
	else
	{
		_Short_dbg_msg szBuffer;

		PrintToCharArray(
			szBuffer, "Eifersucht. InternalSceneImpl. FbxSdkVer = %d.%d.%d %s",
			iSdkMajor, iSdkMinor, iSdkRevision,
			Fbx::Manager::GetVersion()
			);

		PrintToDebugWindowOnly8( szBuffer );
		if ( pSdkImporter->IsFBX() )
		{
			Reign::Standard::ZeroValueArray(
				szBuffer, ARRAYSIZE(szBuffer)
				);
			PrintToCharArray(
				szBuffer, "Eifersucht. InternalSceneImpl. FbxFileVer = %d.%d.%d",
				iFileMajor, iFileMinor, iFileRevision
				);

			PrintToDebugWindowOnly8( szBuffer );
		}
		else
		{
			PrintToDebugWindowOnly8( 
				"Eifersucht. InternalSceneImpl. FbxFileVer = "
				"[NonFbx]" 
				);
		}
	}
#endif

	Safe_Destroy(_m_pScene);
	_m_pScene = Fbx::Scene::Create(
		_Internal_static_impl::_s_pManager, 
		"EifersuchtImportedScene"
		);

	bStatus = pSdkImporter->Import( _m_pScene );
	Fbx::AxisSystem System( Fbx::AxisSystem::eMax );
	System.ConvertScene(_m_pScene);

	Safe_Destroy(pSdkImporter);
	return true;
}

void Eifersucht::Scene::_Internal_impl::_Assign_recursively()
{
	if ( !_m_pScene || !_m_pRef ) return;

	auto pNode = _m_pScene->GetRootNode();
	auto pRoot = _m_pRef->m_pRoot;

	pRoot->m_pImpl->_m_pNode = pNode; // Assign node 
	pRoot->m_pImpl->_Cpy_all(); // Copy the info 

	pRoot->m_pImpl->_Assign_skeleton_recursively(); // Proceed
	pRoot->m_pImpl->_Assign_mesh_recursively(); // Proceed

}


